package com.dongdongshop.service.impl.address;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbAddressMapper;
import com.dongdongshop.mapper.TbAreasMapper;
import com.dongdongshop.mapper.TbCitiesMapper;
import com.dongdongshop.mapper.TbProvincesMapper;
import com.dongdongshop.pojo.address.TbAddress;
import com.dongdongshop.pojo.areas.TbAreas;
import com.dongdongshop.pojo.cities.TbCities;
import com.dongdongshop.pojo.provinces.TbProvinces;
import com.dongdongshop.service.address.AddressService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
@org.springframework.stereotype.Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private TbAddressMapper tbAddressMapper;
    @Autowired
    private TbProvincesMapper tbProvincesMapper;
    @Autowired
    private TbAreasMapper tbAreasMapper;
    @Autowired
    private TbCitiesMapper tbCitiesMapper;

    @Override
    public List<TbAddress> listAddressByUserId(String username) {
        List<TbAddress> tbAddresses = tbAddressMapper.listAddressByUserId(username);
        for (TbAddress address : tbAddresses) {
            TbProvinces provinces = tbProvincesMapper.getProbincesByProId(address.getProvinceId());
            TbCities cities = tbCitiesMapper.getCityByCityId(address.getCityId());
            TbAreas areas = tbAreasMapper.getAreaByAreaId(address.getTownId());
            address.setTownId(areas.getArea());
            address.setProvinceId(provinces.getProvince());
            address.setCityId(cities.getCity());
        }
        return tbAddresses;
    }

    @Override
    public int insertAddress(TbAddress tbAddress) {
        return tbAddressMapper.insertSelective(tbAddress);
    }
}
